<template>
  <PageDialog
    :title="dialogInfo.title"
    v-model:visible="shows"
    :width="dialogInfo.width"
    :bt-loading="dialogInfo.btLoading"
    :bt-list="dialogInfo.btList"
  >
    <div class="dialog-table">
      <PageFilter
        v-model:query="filterInfo.query"
        :filter-list="filterInfo.list"
      >
        <template v-slot:filter-button>
          <div class="dialog-table-header">
            <div class="header-left">
              <div class="lable">产品名称：{{ info.commonName }}</div>
              <div class="lable">生产批号：{{ info.batchNumber }}</div>
            </div>
            <div class="header-right">
              <div class="scan-groups">
                <el-input
                  v-model="filterInfo.keyword"
                  :clearable="true"
                  placeholder="请输入条码/个体码/主码/从码"
                  @keyup.enter.native="search"
                  @input="(e) => (filterInfo.keyword = validSe(e))"
                ></el-input>
                <el-button style="margin-left: 10px" @click="search"
                  >搜索</el-button
                >
              </div>
            </div>
          </div>
          <div>
            <el-button @click="changeNumberAll(true)">一键填充数量</el-button>
            <el-button @click="changeNumberAll(false)">一键清空</el-button>
          </div>
        </template>
      </PageFilter>
    </div>
    <el-form :model="tableInfo" ref="tableForm" style="width: 100%">
      <DialogTable
        :row-class-name="rowClassName"
        :refresh="tableInfo.refresh"
        :init-curpage="tableInfo.initCurpage"
        ref="depotTable"
        v-model:data="tableInfo.data"
        :check-box="false"
        :tab-index="true"
        :pager="false"
        :summary="tableInfo.summary"
        :summaryFieldList="tableInfo.summaryFieldList"
        :handle="tableInfo.handle"
        :listen-height="false"
        :field-list="tableInfo.fieldList"
      >
        <template v-slot:col-price="scope">
          <el-form-item
            v-if="
              scope.row.depotStockId === -2 &&
              !groupList.includes('price') &&
              isSysGroup
            "
            :prop="`data.${scope.$index}.price`"
            :rules="
              scope.$index >= 0
                ? [{ validator: $validate.positiveMoneyZero, trigger: 'blur' }]
                : []
            "
          >
            <el-input-number
              v-model.number="scope.row.price"
              controls-position="right"
              :precision="systemConfig.price_precision"
              :min="0"
              @change="
                changePrice($event, scope.row, `data.${scope.$index}.price`)
              "
              :max="999999999999999"
              :step="1"
            ></el-input-number>
          </el-form-item>
          <p v-else>{{ $fn.TripartiteMethod(scope.row.price) }}</p>
        </template>
        <template v-slot:col-qty="scope">
          <el-form-item class="margin-bottom">
            <div class="page-table-input-number-wrapper">
              <div class="page-table-input-number-left">
                <div class="page-table-input-number-item">
                  <el-form-item
                    :prop="`data.${scope.$index}.largeUnitQty`"
                    :rules="
                      scope.$index >= 0
                        ? [
                            {
                              validator: $validate.positiveAndZero,
                              trigger: 'blur',
                            },
                          ]
                        : []
                    "
                  >
                    <el-input-number
                      style="width: 100%; margintop: 18px"
                      v-model.lazy="scope.row.largeUnitQty"
                      clearable
                      controls-position="right"
                      :precision="
                        scope.row.qtyOperateMode === 0 ||
                        scope.row.qtyOperateMode === 2
                          ? 0
                          : systemConfig.qty_show_precision
                      "
                      @change="
                        changeLarge(
                          $event,
                          scope.row,
                          `data.${scope.$index}.largeUnitQty`
                        )
                      "
                      :min="0"
                      :max="largeMax(scope.row)"
                    />
                  </el-form-item>
                  <div class="page-table-input-number-item-unite">
                    {{ scope.row.unitName }}
                  </div>
                </div>
                <div
                  class="page-table-input-number-item"
                  v-if="
                    scope.row.unitConvertCoefficient > 1 &&
                    scope.row.qtyOperateMode === 2
                  "
                >
                  <el-form-item>
                    <el-input-number
                      style="width: 100%; margintop: 18px"
                      v-model.lazy="scope.row.smallUnitQty"
                      clearable
                      controls-position="right"
                      :precision="0"
                      :min="0"
                      @change="changeSmall($event, scope.row)"
                      :max="smallMax(scope.row)"
                    />
                  </el-form-item>
                  <div class="page-table-input-number-item-unite">
                    {{ scope.row.calcUnitName }}
                  </div>
                </div>
              </div>
              <div
                class="page-table-input-number-right"
                v-if="scope.row.qtyOperateMode === 2"
              >
                {{ $fn.qtyFormatter(scope.row.qty) }}
              </div>
            </div>
          </el-form-item>
        </template>
        <template v-slot:col-batchNumber="scope">
          <el-input
            v-if="
              scope.row.depotStockId === -2 &&
              scope.row.codeType !== 3 &&
              !groupList.includes('batchNumber') &&
              isSysGroup
            "
            v-model="scope.row.batchNumber"
            :maxlength="25"
            @input="(e) => (scope.row.batchNumber = validSe(e))"
            @change="changeBatchNumber($event, scope.row)"
          />
          <p v-else>{{ scope.row.batchNumber || '-' }}</p>
        </template>
        <template v-slot:col-productionDate="scope">
          <el-date-picker
            v-if="
              scope.row.depotStockId === -2 &&
              scope.row.codeType !== 3 &&
              !groupList.includes('productionDate') &&
              isSysGroup
            "
            v-model="scope.row.productionDate"
            value-format="YYYY-MM-DD"
            :disabled-date="productionTimePickerOptions.disabledDate"
            @change="changeProductionDate($event, scope.row)"
          />
          <p v-else>{{ scope.row.productionDate || '-' }}</p>
        </template>
        <template v-slot:col-validityDate="scope">
          <el-date-picker
            v-if="
              scope.row.depotStockId === -2 &&
              scope.row.codeType !== 3 &&
              !groupList.includes('validityDate') &&
              isSysGroup
            "
            v-model="scope.row.validityDate"
            value-format="YYYY-MM-DD"
            :disabled-date="validityTimePickerOptions.disabledDate"
            @change="changeValidityDate($event, scope.row)"
          />
          <p v-else>{{ scope.row.validityDate || '-' }}</p>
        </template>
      </DialogTable>
    </el-form>
  </PageDialog>
</template>

<script setup name="IndividualDetails">
import { getCustomizeSetting } from '@/api/customize'

const { proxy } = getCurrentInstance()
const props = defineProps({
  api: {
    type: Function,
  },
  delApi: {
    type: Function,
  },
  saveApi: {
    type: Function,
  },
  showDialog: {
    type: Boolean,
  },
  query: {
    type: Object,
    default: function () {
      return {}
    },
  },
  isSysGroup: {
    type: Boolean,
    default: true,
  },
  groupList: {
    //分组项
    type: Array,
    default: [],
  },
  info: {
    type: Object,
    default: function () {
      return {}
    },
  },
  mergeCustomizeUserVO: {
    type: Object,
    default: function () {
      return {}
    },
  },
  tableData: {
    type: Array,
    default: function () {
      return []
    },
  },
  tableKey: {
    type: String,
    default: '',
  },
  isConsignBusiness: {
    type: Number,
    default: 0,
  },
})
const formRefs = ref('')
const productionTimePickerOptions = ref({
  disabledDate(time) {
    return time.getTime() > Date.now() - 8.64e6
  },
})
const validityTimePickerOptions = ref({
  disabledDate(time) {
    return time.getTime() < Date.now() - 24 * 60 * 60 * 1000
  },
})
// 过滤相关配置
const systemConfig = ref(proxy.$store.state.permission.systemConfig)
const filterInfo = ref({
  keyword: '',
  query: {
    customizeUserVO: {},
    queryMap: {},
    queryParams: {
      stockAvailableCalcQtyGt: 0,
    },
  },
  list: [{ type: 'slot', value: 'button' }],
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  copyData: [],
  selections: [],
  summary: true,
  summaryFieldList: [
    { value: 'stockQty', formatter: proxy.$fn.qtyFormatter },
    { value: 'qty', formatter: proxy.$fn.qtyFormatter },
    { value: 'diffQty', formatter: proxy.$fn.qtyFormatter },
    { value: 'price', show: 'false' },
    { value: 'sumAmount', formatter: proxy.$fn.TripartiteMethod },
    { value: 'diffSumAmount', formatter: proxy.$fn.TripartiteMethod },
  ],
  fieldList: [],
  handle: {
    fixed: 'right',
    label: '操作',
    width: '120',
    btList: [
      {
        label: '复制行',
        event: addItem,
        show: true,
        ifRender: ifRenderValue,
      },
      {
        label: '删除',
        event: remove,
        show: true,
        popconfirmText: '删除保存后将改变数据,是否继续？',
        ifRender: function (row) {
          return row.stockQty === 0
        },
      },
    ],
  },
})
// 弹窗相关
const dialogInfo = ref({
  title: '选择个体',
  btLoading: false,
  width: '90%',
  btList: [
    { label: '关闭', event: handleClose, show: true },
    { label: '保存', event: handleSave, show: true },
  ],
})
watch(
  () => tableInfo.value.data,
  (val) => {
    tableInfo.value.data.forEach((row) => {
      let smallUnitQty = parseFloat(row.smallUnitQty || 0)
      let unitConvertCoefficient = parseFloat(row.unitConvertCoefficient || 1)
      let price = row.price || 0
      let largeUnitQty = parseFloat(row.largeUnitQty || 0)
      let calcQty = proxy.$fn.accAdd(
        proxy.$fn.accMul(largeUnitQty, unitConvertCoefficient),
        smallUnitQty
      )
      let qty = proxy.$fn.accDiv(calcQty, unitConvertCoefficient)
      let sumAmount = proxy.$fn.accDiv(
        proxy.$fn.accMul(calcQty, price),
        unitConvertCoefficient
      )
      let diffCalcQty = proxy.$fn.Subtr(calcQty, row.stockCalcQty)
      let diffSumAmount = proxy.$fn.accDiv(
        proxy.$fn.accMul(diffCalcQty, price),
        unitConvertCoefficient
      )
      let diffQty = proxy.$fn.accDiv(row.diffCalcQty, unitConvertCoefficient)
      row['calcQty'] = calcQty
      row['sumAmount'] = sumAmount
      row['diffSumAmount'] = diffSumAmount
      row['qty'] = qty
      row['diffCalcQty'] = diffCalcQty
      row['diffQty'] = diffQty
    })
    tableInfo.value.copyData.forEach((row) => {
      let smallUnitQty = parseFloat(row.smallUnitQty || 0)
      let unitConvertCoefficient = parseFloat(row.unitConvertCoefficient || 1)
      let price = row.price || 0
      let largeUnitQty = parseFloat(row.largeUnitQty || 0)
      let calcQty = proxy.$fn.accAdd(
        proxy.$fn.accMul(largeUnitQty, unitConvertCoefficient),
        smallUnitQty
      )
      let qty = proxy.$fn.accDiv(calcQty, unitConvertCoefficient)
      let sumAmount = proxy.$fn.accDiv(
        proxy.$fn.accMul(calcQty, price),
        unitConvertCoefficient
      )
      let diffCalcQty = proxy.$fn.Subtr(calcQty, row.stockCalcQty)
      let diffSumAmount = proxy.$fn.accDiv(
        proxy.$fn.accMul(diffCalcQty, price),
        unitConvertCoefficient
      )
      let diffQty = proxy.$fn.accDiv(row.diffCalcQty, unitConvertCoefficient)
      row['calcQty'] = calcQty
      row['sumAmount'] = sumAmount
      row['diffSumAmount'] = diffSumAmount
      row['qty'] = qty
      row['diffCalcQty'] = diffCalcQty
      row['diffQty'] = diffQty
    })
  },
  { deep: true }
)
const shows = computed({
  get() {
    return props.showDialog
  },
  set(val) {
    proxy.$emit('update:showDialog', val) // 更新父组件shows
  },
})

function ifRenderValue() {
  return (
    props.info.codeType !== 3 &&
    props.isSysGroup &&
    props.info.id &&
    props.isConsignBusiness === 0
  )
}
function search() {
  const search = filterInfo.value.keyword
  if (search) {
    let copyData = proxy.$fn.deepClone(tableInfo.value.copyData)
    tableInfo.value.data = copyData.filter((data) => {
      return Object.keys(data).some((key) => {
        if (key === 'qrCode') {
          return String(data[key]).indexOf(search) > -1
        } else if (key === 'uniqueCode') {
          return String(data[key]).indexOf(search) > -1
        } else if (key === 'factorySubCode') {
          return String(data[key]).indexOf(search) > -1
        } else if (key === 'factoryMainCode') {
          return String(data[key]).indexOf(search) > -1
        }
      })
    })
  } else {
    tableInfo.value.data = proxy.$fn.deepClone(tableInfo.value.copyData)
  }
}
function smallMax(row) {
  if (props.isConsignBusiness === 1) {
    let largeUnitQty = parseInt(row.stockCalcQty / row.unitConvertCoefficient)
    if (largeUnitQty === 0 || row.largeUnitQty === largeUnitQty) {
      return parseInt(row.stockCalcQty % row.unitConvertCoefficient)
    } else {
      return row.unitConvertCoefficient - 1
    }
  } else {
    return row.unitConvertCoefficient - 1
  }
}
function largeMax(row) {
  //2系统唯一码，3原厂码
  // if (row.qtyOperateMode === 2) {
  if (props.isConsignBusiness === 1) {
    if (row.qtyOperateMode === 2) {
      return parseInt(row.stockCalcQty / row.unitConvertCoefficient)
    } else {
      return row.stockCalcQty
    }
  } else {
    if (row.codeType === 2) {
      if (row.stockQty === 0) {
        return 99999999
      } else {
        return 1
      }
    } else if (row.codeType === 3) {
      return 1
    } else {
      return 99999999
    }
  }
}
function changePrice(e, row, str) {
  proxy.$refs.tableForm.clearValidate(str)
  let item = tableInfo.value.copyData.find(
    (item) => item.tableIndexFlag === row.tableIndexFlag
  )
  item.price = e
}
function changeLarge(e, row, str) {
  proxy.$refs.tableForm.clearValidate(str)
  tableInfo.value.copyData.forEach((el, index) => {
    if (el.tableIndexFlag === row.tableIndexFlag) {
      tableInfo.value.copyData[index] = row
    }
  })
  if ((row.qtyOperateMode === 2 && row.stockQty > 0) || row.codeType === 3) {
    if (e === 1) {
      row.smallUnitQty = 0
    }
  }
}
function changeBatchNumber(e, row) {
  let item = tableInfo.value.copyData.find(
    (item) => item.tableIndexFlag === row.tableIndexFlag
  )
  item.batchNumber = e
}
function changeProductionDate(e, row) {
  let item = tableInfo.value.copyData.find(
    (item) => item.tableIndexFlag === row.tableIndexFlag
  )
  item.productionDate = e
}
function changeValidityDate(e, row) {
  let item = tableInfo.value.copyData.find(
    (item) => item.tableIndexFlag === row.tableIndexFlag
  )
  item.validityDate = e
}
function changeSmall(e, row) {
  if (row.qtyOperateMode === 2) {
    let item = tableInfo.value.copyData.find(
      (item) => item.tableIndexFlag === row.tableIndexFlag
    )
    item.smallUnitQty = e
    if ((row.codeType === 2 && row.stockQty > 0) || row.codeType === 3) {
      if (e > 0) {
        row.largeUnitQty = 0
        item.largeUnitQty = 0
      }
    }
  }
}
// 获取列表
function getList() {
  props.api(filterInfo.value.query).then((res) => {
    if (res && res.success) {
      let data = res.data
      let groupList = props.groupList
      data.forEach((item) => {
        if (groupList.includes('price')) {
          item.price = props.info.price
        }
        if (groupList.includes('batchNumber')) {
          item.batchNumber = props.info.batchNumber
        }
        if (groupList.includes('productionDate')) {
          item.productionDate = props.info.productionDate
        }
        if (groupList.includes('validityDate')) {
          item.validityDate = props.info.validityDate
        }
        item.tableIndexFlag = proxy.$fn.getUid()
      })
      tableInfo.value.data = data
      tableInfo.value.copyData = proxy.$fn.deepClone(data)
    }
  })
}
// 关闭弹窗前的回调
function handleClose() {
  shows.value = false
  proxy.$emit('reset')
}
function istRemoveRepeat(array) {
  //去重
  let result = [array[0]]
  for (let i = 1; i < array.length; i++) {
    let item = array[i]
    let repeat = false
    for (let j = 0; j < result.length; j++) {
      if (generateProductKey(item) == generateProductKey(result[j])) {
        repeat = true
        break
      }
    }
    if (!repeat) {
      result.push(item)
    }
  }
  return result
}
function handleSave() {
  proxy.$fn.checkForm(formRefs.value).then(() => {
    let commonName = tableInfo.value.copyData
      .filter(
        (detail) =>
          detail.depotStockId === -2 &&
          detail.needBatchNumber == '1' &&
          ((!detail.batchNumber && detail.batchNumber !== '0') ||
            !detail.productionDate ||
            !detail.validityDate)
      )
      .map((detail) => detail.commonName)
    if (commonName.length) {
      proxy.$alert(`请输入产品【${commonName}】的批号效期`, '操作提示', {
        confirmButtonText: '确定',
      })
      return
    }

    if (props.isSysGroup) {
      if (props.info.codeType === 2 || props.info.codeType === 3) {
        let details = tableInfo.value.copyData.filter(
          (row) => row.depotStockId === -2
        )
        if (
          details.length &&
          details.length !== istRemoveRepeat(details).length
        ) {
          proxy.$alert(
            `存在复制行不能与其他行批号效期${
              props.info.codeType === 4 ? '' : '价格'
            }相同，请修改`,
            '操作提示',
            { confirmButtonText: '确定' }
          )
          return
        }
      } else if (props.info.codeType === 4) {
        let details = tableInfo.value.copyData
        if (
          details.length &&
          details.length !== istRemoveRepeat(details).length
        ) {
          proxy.$alert(
            `存在复制行不能与其他行批号效期价格相同，请修改`,
            '操作提示',
            { confirmButtonText: '确定' }
          )
          return
        }
      } else if (props.info.codeType === 1) {
        if (
          tableInfo.value.copyData.length !==
          istRemoveRepeat(tableInfo.value.copyData).length
        ) {
          proxy.$alert(
            `存在复制行不能与其他行批号效期价格相同，请修改`,
            '操作提示',
            { confirmButtonText: '确定' }
          )
          return
        }
      }
    }
    let smallUnitQty = 0
    let largeUnitQty = 0
    let calcQty = 0
    let sumAmountCalc = 0
    let diffCalcQty = 0
    let diffSumAmountCalc = 0
    let unitConvertCoefficient = props.info.unitConvertCoefficient
    tableInfo.value.copyData.forEach((item) => {
      calcQty = proxy.$fn.accAdd(calcQty, item.calcQty)
      diffCalcQty = proxy.$fn.accAdd(diffCalcQty, item.diffCalcQty)
      smallUnitQty = proxy.$fn.accAdd(smallUnitQty, item.smallUnitQty)
      largeUnitQty = proxy.$fn.accAdd(largeUnitQty, item.largeUnitQty)
      sumAmountCalc = proxy.$fn.accAdd(
        sumAmountCalc,
        proxy.$fn.accMul(item.price || 0, item.calcQty)
      )
      diffSumAmountCalc = proxy.$fn.accAdd(
        diffSumAmountCalc,
        proxy.$fn.accMul(item.price || 0, item.diffCalcQty)
      )
    })
    let data = {
      ...props.info,
      calcQty: calcQty,
      diffCalcQty: diffCalcQty,
      qty: proxy.$fn.accDiv(calcQty, unitConvertCoefficient),
      diffQty: proxy.$fn.accDiv(diffCalcQty, unitConvertCoefficient),
      sumAmount: proxy.$fn.accDiv(sumAmountCalc, unitConvertCoefficient),
      diffSumAmount: proxy.$fn.accDiv(
        diffSumAmountCalc,
        unitConvertCoefficient
      ),
      smallUnitQty: smallUnitQty,
      largeUnitQty: largeUnitQty,
      details: proxy.$fn.filterBlacklist(tableInfo.value.copyData),
    }
    if (!data.groupConcatIds) {
      data.groupConcatIds = tableInfo.value.copyData
        .map((item) => item.id)
        .join(',')
    }
    let params
    if (!props.info.id) {
      params = data
    } else {
      params = {
        data: data,
        groupFieldList: props.groupList,
        customizeUserVO: props.mergeCustomizeUserVO,
      }
    }
    props.saveApi(params).then((res) => {
      if (res && res.success) {
        res.data.tableIndex = props.info.tableIndex
        res.data.key = props.info.key
        //用于判断是否删除父列表的数据（当弹窗数据都被删除后清除父列表数据）
        res.data.isCleanProps = tableInfo.value.data.length === 0
        if (props.isSysGroup) {
          delete res.data.details
          proxy.$emit('callback', res.data)
        } else {
          delete res.data.details
          proxy.$emit('callback', res.data)
        }
        handleClose()
      }
    })
  })
}
function generateProductKey(item) {
  let key = ''
  //一物一码带上码校验
  if (item.codeType === 2 || item.codeType === 3) {
    key =
      item.productId +
      '&' +
      item.batchNumber +
      '&' +
      item.validityDate +
      '&' +
      item.productionDate +
      '&' +
      item.price +
      '&' +
      item.qrCode +
      '&' +
      item.uniqueCode +
      '&' +
      item.factorySubCode +
      '&' +
      item.factoryMainCode +
      '&' +
      item.storeBatchNumber
  } else if (item.codeType === 4) {
    key =
      item.productId +
      '&' +
      item.batchNumber +
      '&' +
      item.validityDate +
      '&' +
      item.productionDate +
      '&' +
      item.price +
      '&' +
      item.qrCode +
      '&' +
      item.uniqueCode +
      '&' +
      item.factorySubCode +
      '&' +
      item.factoryMainCode +
      '&' +
      item.storeBatchNumber
  } else {
    key =
      item.productId +
      '&' +
      item.batchNumber +
      '&' +
      item.validityDate +
      '&' +
      item.productionDate +
      '&' +
      item.price +
      '&' +
      item.supplierId +
      '&' +
      item.storeBatchNumber
  }
  return key
}
function changeNumberAll(isAll) {
  let tableIndexFlagList = tableInfo.value.data.map(
    (item) => item.tableIndexFlag
  )
  if (isAll) {
    tableInfo.value.data.forEach((row) => {
      if (row.stockQty > 0) {
        if (row.qtyOperateMode === 2) {
          row.largeUnitQty = parseInt(
            row.stockCalcQty / row.unitConvertCoefficient
          )
          row.smallUnitQty = parseInt(
            row.stockCalcQty % row.unitConvertCoefficient
          )
        } else {
          row.largeUnitQty = row.stockQty
          row.smallUnitQty = 0
        }
      }
    })
    tableInfo.value.copyData.forEach((row) => {
      if (tableIndexFlagList.includes(row.tableIndexFlag)) {
        if (row.stockQty > 0) {
          if (row.qtyOperateMode === 2) {
            row.largeUnitQty = parseInt(
              row.stockCalcQty / row.unitConvertCoefficient
            )
            row.smallUnitQty = parseInt(
              row.stockCalcQty % row.unitConvertCoefficient
            )
          } else {
            row.largeUnitQty = row.stockQty
            row.smallUnitQty = 0
          }
        }
      }
    })
  } else {
    tableInfo.value.data.forEach((row) => {
      row.largeUnitQty = 0
      row.smallUnitQty = 0
    })
    tableInfo.value.copyData.forEach((row) => {
      if (tableIndexFlagList.includes(row.tableIndexFlag)) {
        row.largeUnitQty = 0
        row.smallUnitQty = 0
      }
    })
  }
}
function remove(row) {
  tableInfo.value.data = tableInfo.value.data.filter((item) => item !== row)
  tableInfo.value.copyData = tableInfo.value.data.filter(
    (item) => item.tableIndexFlag !== row.tableIndexFlag
  )
}
function addItem(row, index) {
  let newData = {
    ...row,
    diffQty: 1,
    diffCalcQty: row.unitConvertCoefficient,
    qty: 1,
    calcQty: row.unitConvertCoefficient,
    stockQty: 0,
    stockCalcQty: 0,
    largeUnitQty: 1,
    smallUnitQty: 0,
    frozenCalcQty: 0,
    frozenQty: 0,
    sumAmount: row.price,
    diffSumAmount: row.price,
    id: null,
    tableIndexFlag: proxy.$fn.getUid(),
    depotStockId: -2,
    qrCode: '',
    uniqueCode: '',
    operateSingle: !tableInfo.value.isSysGroup,
    isConsignment: '',
    isConsignmentValue: '',
    factoryMainCode: '',
    factorySubCode: '',
    storeBatchNumber: '',
  }
  delete newData.key
  tableInfo.value.data.splice(index + 1, 0, newData)
  let itemIndex = tableInfo.value.copyData.findIndex(
    (item) => item.tableIndexFlag === row.tableIndexFlag
  )
  if (index > -1) {
    tableInfo.value.copyData.splice(itemIndex + 1, 0, newData)
  }
}
function getCustomize() {
  let param = { tableKey: props.tableKey }
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data
      tableInfo.value.fieldList = proxy.$fn.setFieldList(
        tableInfo.value.fieldList,
        data.detail
      )
      filterInfo.value.query.customizeUserVO = data
      filterInfo.value.query.queryMap = props.query
      if (props.isSysGroup) {
        if (props.info.id) {
          getList()
        } else {
          props.info.tableIndexFlag = proxy.$fn.getUid()
          tableInfo.value.data = [props.info]
          tableInfo.value.copyData = proxy.$fn.deepClone(tableInfo.value.data)
        }
      } else {
        props.info.tableIndexFlag = proxy.$fn.getUid()
        tableInfo.value.data = [props.info]
        tableInfo.value.copyData = proxy.$fn.deepClone(tableInfo.value.data)
      }
    }
  })
}
function rowClassName({ row }) {
  let className = 'row' + row.id
  if (row.depotStockId === -2) {
    className += ' warning-row'
  }
  if (row.hasOwnProperty('currentScanData') && row.currentScanData) {
    className += ' light-row'
  }
  return className
}

let obj = {
  qty: {
    label: '数量',
    value: 'qty',
    type: 'slot',
    minWidth: '250px',
    show: false,
    hidden: true,
  },
  price: {
    label: '单价',
    value: 'price',
    minWidth: '150px',
    type: 'slot',
    permission: !proxy.$permission('inventories:price'),
    show: false,
    hidden: true,
  },
  defaultSupplierPrice: {
    label: '默认供应商价格',
    value: 'defaultSupplierPrice',
    type: 'column',
    formatter: proxy.$fn.TripartiteMethod,
    permission: !proxy.$permission('inventories:price'),
    hidden: true,
    show: false,
  },
  sumAmount: {
    label: '总金额',
    value: 'sumAmount',
    type: 'column',
    formatter: proxy.$fn.TripartiteMethod,
    permission: !proxy.$permission('inventories:price'),
    show: false,
    hidden: true,
  },
  diffQty: {
    label: '差异数量',
    value: 'diffQty',
    type: 'column',
    formatter: proxy.$fn.qtyFormatter,
    show: false,
    hidden: true,
  },
  diffSumAmount: {
    label: '差异金额',
    value: 'diffSumAmount',
    type: 'column',
    permission: !proxy.$permission('inventories:price'),
    formatter: proxy.$fn.TripartiteMethod,
    show: false,
    hidden: true,
  },
  batchNumber: {
    label: '生产批号',
    value: 'batchNumber',
    type: 'slot',
    show: false,
    hidden: true,
  },
  productionDate: {
    label: '生产日期',
    value: 'productionDate',
    type: 'slot',
    minWidth: '150px',
    show: false,
    hidden: true,
  },
  validityDate: {
    label: '失效日期',
    value: 'validityDate',
    minWidth: '150px',
    type: 'slot',
    show: false,
    hidden: true,
  },
}
tableInfo.value.fieldList = Object.values({
  ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
  ...obj,
})
getCustomize()
onMounted(() => {
  proxy.$nextTick(() => {
    formRefs.value = [proxy.$refs['tableForm']]
  })
})
</script>

<style scoped lang="scss">
:deep(.filter-item) {
  width: 100%;
}
.text-style {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.scan-groups {
  display: flex !important;
  align-items: center;
}
</style>
